package com.huaqin.punan.gsensor;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity implements SensorEventListener {

	private TextView mytv;
	private TextView mytv2;
	
	private float x = 0;
	private float y = 0;
	private float z = 0;
	
	private float[] tempx = new float[20];
	private float[] tempy = new float[20];
	private float[] tempz = new float[20];
	
	private SensorManager mSensorManager;
    private Sensor mAccelerometer;
	
	private Handler mHandler;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
        mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
        mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
		
        mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
        
		mytv = (TextView)findViewById(R.id.mytv1);
		mytv2 = (TextView)findViewById(R.id.mytv2);
		
		final Timer timer = new Timer();
		timer.schedule(new TimerTask(){
			int i = 0;
			@Override
			public void run() {
				// TODO Auto-generated mezthod stub
				Message msg = new Message();
				msg.what = 0;
				msg.arg1 = i;
				mHandler.sendMessage(msg);
				
				i++;
				if(i>19) timer.cancel();
				
			}}, 1000, 1000);
		
		mHandler = new Handler(){

			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				
				float sumX = 0;
				float sumY = 0;
				float sumZ = 0;
				
				float averageX = 0;
				float averageY = 0;
				float averageZ = 0;
				
				int i = msg.arg1;
				
				if(msg.what == 0){
					tempx[i] = x;
					tempy[i] = y;
					tempz[i] = z;
					
					System.out.println("tempx["+i+"]="+x+"  tempy["+i+"]="+y+"  tempz["+i+"] = "+z);
				}
				
				if(i==19){
					for (int j=0; j<20; j++){
						sumX = sumX +tempx[i];
						sumY = sumY +tempy[i];
						sumZ = sumZ +tempz[i];
					}
					
					averageX = sumX/20;
					averageY = sumY/20;
					averageZ = sumZ/20;
					
					System.out.println("sumX="+sumX+" sumY="+sumY+" sumX="+sumZ);
					
					mytv2.setText("averageX="+averageX+"\naverageY="+averageY+"\naverageZ="+averageZ);
					
				}
				
			}
		};
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		// TODO Auto-generated method stub
		
		x = event.values[0];
		y = event.values[1];
		z = event.values[2];
		
		mytv.setText("x="+x+"  y="+y+"  z="+z);
		
	}

}
